﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GtsServer.Service.Model;
using GtsServer.Model;
using GtsServer.DAL;
using System.Configuration;
using System.Security.Cryptography;
using System.IO;
using System.Web;
using System.Text;

namespace GtsServer.Charge.Page
{
    public class Api
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SendSms();
        }
        public void SendSms()
        {
            string url=string.Empty;
            string modelist = "";
            int orderId=0;
            string input = "{\"pid\":100,\"log\":\"|zfb_2_'2013-01-17   07:02:25'\",\"key\":\"UEsDBBQACAgIAERUMUIAAAAAAAAAAAAAAAAHAAAAa2V5LmRhdO2UzQ6CMBCEX4UbJ5NufyjlZYghiERRI3gw4eGtFBfSbiXePc+Xzcx0t2N1PNXPMitTzkDsGOxAJ0nCdMF4wVU6Ol3TevbRGa3rdDx0b50bGjAI0BMEbEwQHAGgAbE1QW0BmAIECUhma7j08R5WAKcBtQD0U0h0CZIGcowRcYldSxpQi0uR+4SxIQqJXUKQYwLUZKJ63Ifq2l7CNjyobprSaBkkXmG38\"45tF3dl1p8G\"c39ZOp4J4dYOKnYAEomIxvoQOy+BY6QMe30AF5fAuB2XNeuQxMvAFpdxkBP8YMLH+C3+gMYE7hP98CuJ\"P\"3VmPU9fUEsHCMOGDtL1AAAAWgUAAFBLAQIUABQACAAIAERUMULDhg7S9QAAAFoFAAAHAAAAAAAAAAAAAAAAAAAAAABrZXkuZGF0UEsFBgAAAAABAAEANQAAACoBAAAAAA==\",\"request\":{\"charge\":{\"money\":100,\"charge_id\":1,\"charge_mode\":3,\"app_id\":1,\"coin_type\":1}},\"info\":{\"sim\":{\"plmn\":\"\",\"imei\":\"A0000039C4070E\",\"imsi\":\"460036430000080\"},\"version\":{\"reserved\":5,\"minor\":8,\"major\":1},\"charge\":{\"type\":0}}}";
            ChargingSzfRequest charg = new ChargingSzfRequest();
            charg = charg.Init(input);
            int mode = charg.charge.mode;
            int appID = charg.charge.appid;
            int money = charg.charge.money;
            int coinType = charg.charge.coinType;
            OrderDAL orderdal = new OrderDAL();
            int imsi = Convert.ToInt32(charg.info.sim.imsi);
            switch (mode)
            {
                 //神州付
                case 2:                        
                    string card = charg.szf.card;//卡号（神州付）
                    string pwd = charg.szf.pwd; //密码
                    int price = charg.szf.price; //面值
                    string plmn = charg.info.sim.plmn;//sim卡的plmn号码
                    int op = charg.szf.op;//运营商
                    //Order orderModels = new Order()
                    //{
                    //    Createtime = Convert.ToDateTime(DateTime.Now.ToString()),
                    //    Imsi = imsi,
                    //    Price = money,
                    //    State = 0,
                    //    ExpenseID = mode,
                    //    CardNum = card,
                    //    CardPwd = pwd,
                    //    CardPrice = price,
                    //    Op = op
                      
                    //};
                    string vesion="1";//版本号()
                    //int id = orderdal.GetMaxId();
                    string cardInfo="";//神州付所有信息
                    string MerUserName = "";//()
                    string MerUserMail = "";//()
                    string PrivateField = "";//()
                    string VerifyType = "";//()
                    string md5String = "";//()
                    string MerId="2088601123663503";
                    string returnUrl=ConfigurationManager.AppSettings["ConnectionString"].ToString();
                    string orderIds = DateTime.Now.ToString("yyyyMMdd");//+ MerId+ id.ToString();
                    string urls = ConfigurationManager.AppSettings["ConnectionString"].ToString();
                    url = string.Format("{0}?version={1}&merId={2}&payMoney={3}&orderId={4}&returnUrl={5}&cardInfo={6}&merUserName={7}&merUserMail={8}&privateField={9}&verifyType={10}&cardTypeCombine={11}&md5String={12}", urls, vesion, MerId, money, orderIds, returnUrl, cardInfo, MerUserName, MerUserMail, PrivateField, VerifyType, op, md5String);
                    modelist = "\"szf_list\":{\"szf\":{\"szf_url\":\"" + url + "\",\"szf_order\":\"" + orderIds + "\"}},";
                    break;
                case 3:
                    //Order orderModel = new Order()
                    //{
                    //    Createtime = Convert.ToDateTime(DateTime.Now.ToString()),//创建订单号时间
                    //    Imsi = imsi,  //手机imsi码
                    //    Price = money,//支付金额
                    //    State = 0,    //支付状态
                    //    ExpenseID=mode, //付费方式（0，1,2）
                    //};
                    //orderId = orderdal.GetMaxId();
                    //合作商户ID  
                    string partner = "160629";
                    //卖家帐号
                    string seller = "";
                    // 外部交易号 这里取当前时间，商户可根据自己的情况修改此参数，但保证唯一性
                    string outTradeNo = orderId.ToString();
                    //获取商品名称
                    string subject = money / 100 + "元" + (coinType == 1 ? "金" : "比赛") + "币";
                    //获取商品描述
                    string body = money / 100 + "元" + (coinType == 1 ? "金" : "比赛") + "币";
                    //获取商品金额
                    string totalFee = (money / 100.00).ToString();
                    //notify_url通知地址
                    string notify_url = ConfigurationManager.AppSettings["ConnectionString"].ToString();
                    //组装待签名数据
                    string signData = "partner=\\" + "\"" + partner + "\\\"";
                    signData += "&";
                    signData += "seller=\\" + "\"" + seller + "\\\"";
                    signData += "&";
                    signData += "out_trade_no=\\" + "\"" + outTradeNo + "\\\"";
                    signData += "&";
                    signData += "subject=\\" + "\"" + subject + "\\\"";
                    signData += "&";
                    signData += "body=\\" + "\"" + body + "\\\"";
                    signData += "&";
                    signData += "total_fee=\\" + "\"" + totalFee + "\\\"";
                    signData += "&";
                    signData += "notify_url=\\" + "\"" + notify_url + "\\\"";
                    modelist = "\"zfb_list\":{\"zfb\":{\"zfb_url\":\"" + signData + "\"}},";
                    break;

            };
        }
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="str"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public string DesEncode(string str, string key)
        {
            byte[] strbyte = Encoding.UTF8.GetBytes(str);
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            des.Mode = CipherMode.ECB;
            des.Key = Convert.FromBase64String(key);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(strbyte, 0, strbyte.Length);
            cs.FlushFinalBlock();
            return Convert.ToBase64String(ms.ToArray());
        }
        
    }
}
